package servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResultsServlet extends HttpServlet 
{
		private static final long serialVersionUID = 1L;
		
		private void doProcessing(HttpServletRequest req, HttpServletResponse resp)
				throws IOException, ServletException
		{
			String searchForm = req.getParameter("search");
			String searchQuery = null;
			
			if(searchForm == null || searchForm.equals("2")) // This must be the first time they've requested a search
			{ 
				RequestDispatcher dispatcher = req.getRequestDispatcher("search.jsp");
				dispatcher.forward(req, resp);
			}
			else if(searchForm.equals("1"))
			{
				searchQuery = req.getParameter("searchQuery");
				req.getSession().setAttribute("searchQuery", searchQuery);
				
				RequestDispatcher dispatcher = req.getRequestDispatcher("search.jsp");
				dispatcher.forward(req, resp);
			}
		}
		
		@Override
		protected void doGet(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException 
		{
			doProcessing(req, resp);
		}
		
		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException 
		{
			doProcessing(req, resp);
		}
}